﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AniMover :Mover {

    private Animator animator;//获取动画
    [SerializeField, Tooltip("y轴浮动大小")]
    private float ySize=0;
    [SerializeField, Tooltip("x轴浮动大小")]
    private float xSize=0;
    private Transform sui;
 
    // Use this for initialization
    void Start () {
        animator = selfTransform.GetChild(0).GetComponent<Animator>();
        sui = GameManager.Instance.Sui.transform;

    }
    public override void Normal_Move()
    {
        base.Normal_Move();
        if (Arrived())
        {
            if (sui == null) return;
            ChangeAni(sui.position  );
        }
        else
        {
            ChangeAni(tagertPos);

        }
    }
    private void ChangeAni(Vector3 p)
    {
        if (animator != null)//先判断有没有动画机
        {
         
            if (p.y <= selfTransform.position.y+ySize&&p.y>=selfTransform.position.y-ySize)//当目标位置的y轴等于自身的y轴时，再判断在左边还是右边，改变动画机参数
            {
                if (p.x > selfTransform.position.x+xSize) animator.SetInteger("dir", 0);
                
                else if(p.x<selfTransform.position.x-xSize) animator.SetInteger("dir", 4);
            }
            else if (!JudgeGT(p, selfTransform.position, 1, 3, 2))//判断是否在上边还是下边
                JudgeGT(selfTransform.position, p, 5, 7, 6);
        }
    }
    /// <summary>
    /// 当a位置的y轴大于b位置的y轴
    /// c为目标位置的x轴大于自身位置的x轴的时候所换的动画参数
    /// d为目标位置的x轴小于自身位置的x轴的时候所换的动画参数
    /// e为目标位置的x轴等于自身位置的x轴的时候所换的动画参数
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <param name="c"></param>
    /// <param name="d"></param>
    /// <param name="e"></param>
    /// <returns></returns>
    private bool JudgeGT(Vector3 a, Vector3 b, int c, int d, int e)
    {
        if (a.y > b.y)
        {
            if (a.x > b.x+xSize)
            {
                animator.SetInteger("dir", c);
            }
            else if (a.x < b.x-xSize) animator.SetInteger("dir", d);
            else animator.SetInteger("dir", e);
            return true;
        }
        return false;
    }


    private void OnDrawGizmos()
    {
        Vector3 temp = tagertPos;
        temp.z = -90;
        Gizmos.color = Color.red;
        Gizmos.DrawSphere(tagertPos, 0.5f);

    }
}
